package com.ruoyi.program.utils;

import com.ruoyi.common.config.RuoYiConfig;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.file.Files;

public class FileMergeUtils {
    public static String merge(String fileMd5, String fileName, int totalChunks) throws IOException {
        // 1. 缓存目录
//        String cacheDir = RuoYiConfig.getcachePath() + File.separator + fileMd5;
//        // 2. 正式文件目录（upload）
//        String uploadDir  = RuoYiConfig.getUploadPath();
//        FileUtils.createDirectory(uploadDir);
//        String targetPath = uploadDir + File.separator + fileName;
//        // 3. 顺序合并
//        try (RandomAccessFile writer = new RandomAccessFile(targetPath, "rw")) {
//            for (int i = 0; i < totalChunks; i++) {
//                File chunk = new File(cacheDir, String.valueOf(i));
//                if (!chunk.exists()) {
//                    throw new IOException("缺失分片 -> index = " + i);
//                }
//                byte[] bytes = Files.readAllBytes(chunk.toPath());
//                writer.write(bytes);
//            }
//        }
//
//        // 4. 删除缓存
//        FileUtils.deleteDirectory(new File(cacheDir));
//
//        // 5. 返回相对路径（供前端拼接访问地址）
        return "fileName";
    }
}